<!-- 事件日志 -->
<template>
  <div class="dashboard-editor-container">
    <div>
      <div style="display:flex;justify-content:space-between;">
        <partitionTree :nodeData="nodeData" :expandArray="expandArray" :addTemplate="false" :defaultExpandAll="false"
          dialogName="SysmonDnsLog" @handleNodeClick="handleNodeClick" ref="partitionTree"
          @toggleVisibility="toggleVisibility">
        </partitionTree>
        <div :style="{ width: containerWidth }" class="box"
          style="background:#fff;border: 1px solid #ddd;border-radius: 4px;box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);">
          <el-row style="background: #fff; padding: 16px 16px 0;">
            <connsearch ref="connsearch" :formOptions="formOptions" @search="search" @reset="reset">
              <el-button slot="buttonContent" size="small" type="success" style="margin-left: 10px;"
                @click="ParseTodayWindowsProc()">分析今日绩效</el-button>
              <el-button slot="buttonContent" size="small" type="success" style="margin-left: 10px;"
                @click="openPDF">导出PDF</el-button>
            </connsearch>
            <div class="tabclick">
              <div class="tabitem" @click="handleClick()">
                <span><i :class="tab ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></span>
              </div>
            </div>
            <el-row>
              <el-tabs type="border-card" v-if="tab" style="margin-top:16px;" @tab-click="tabClick">
                <el-tab-pane label="按资产使用时长统计分析" lazy>
                  <el-row :gutter="20">
                    <el-col :xs="24" :sm="24" :lg="24">
                      <piechart :chartData="pie1Data" @pieclick="echartsClick" title="按资产使用时长统计分析" />
                    </el-col>
                  </el-row>
                </el-tab-pane>
                <el-tab-pane label="按部门使用时长统计分析" lazy>
                  <el-col :xs="24" :sm="24" :lg="24">
                    <piechart :chartData="pie2Data" @pieclick="echartsClick" title="按部门使用时长统计分析" />
                  </el-col>
                </el-tab-pane>
                <el-tab-pane label="按软件分类使用时长统计分析" lazy>
                  <el-col :xs="24" :sm="24" :lg="24">
                    <piechart :chartData="pie3Data" @pieclick="echartsClick" title="按软件分类使用时长统计分析" />
                  </el-col>
                </el-tab-pane>
                <el-tab-pane label="按应用软件使用时长统计分析" lazy>
                  <el-col :xs="24" :sm="24" :lg="24">
                    <piechart :chartData="pie4Data" @pieclick="echartsClick" title="按应用软件使用时长统计分析" />
                  </el-col>
                </el-tab-pane>
              </el-tabs>
            </el-row>
            <el-col :xs="{ span: 24 }" :sm="{ span: 24 }" :md="{ span: 24 }" :lg="{ span: 24 }" :xl="{ span: 24 }"
              style="padding-right: 8px;margin:16px 0;">
              <el-tabs type="border-card" v-model="tabTitle2" @tab-click="tabClick2">
                <el-tab-pane key="概要分析" label="概要分析" name="概要分析" lazy>
                  <el-table stripe border :data="tableData" style="width: 100%; margin-top: 15px">
                    <el-table-column label="序号" width="60" type="index"
                      :index='(index) => { return (index + 1) + (this.pageNum - 1) * this.pageSize }'>
                    </el-table-column>
                    <el-table-column label="部门" show-overflow-tooltip prop="Department" width="120px"></el-table-column>
                    <el-table-column label="资产名称" show-overflow-tooltip prop="DeviceName"
                      width="160px"></el-table-column>
                    <el-table-column label="使用者" show-overflow-tooltip prop="UserName" width="120px"></el-table-column>
                    <el-table-column label="使用时长" prop="RunningSeconds" width="160" show-overflow-tooltip>
                      <template slot-scope="{row}">
                        {{ formatDuration(row.RunningSeconds) }}
                      </template>
                    </el-table-column>
                    <el-table-column label="软件分类" prop="Class" show-overflow-tooltip></el-table-column>
                    <el-table-column label="软件名称" prop="ProcName" show-overflow-tooltip></el-table-column>
                    <el-table-column label="安全等级" prop="Level" width="100px" show-overflow-tooltip></el-table-column>
                    <el-table-column label="工作属性" prop="Property" width="100px" show-overflow-tooltip>
                      <template slot-scope="{row}">
                        <span v-show="row.Property === 0">初始</span>
                        <span v-show="row.Property === 1">工作</span>
                        <span v-show="row.Property === 2">娱乐</span>
                        <span v-show="row.Property === 3">其他</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="执行文件" prop="ProcExe" show-overflow-tooltip></el-table-column>
                    <el-table-column label="操作" width="120px" fixed="right">
                      <template slot-scope="scope">
                        <el-button size="mini" type="success" @click="detailsChange(scope.row)">加载详情</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                  <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page=pageNum :page-size=pageSize background
                    layout="total, prev, pager, sizes, next, jumper" :total="total">
                  </el-pagination>
                </el-tab-pane>
                <el-tab-pane key="加载明细" label="加载明细" name="加载明细" lazy>
                  <el-table stripe border :data="tableData2" style="width: 100%;">
                    <el-table-column label="序号" width="60" type="index"
                      :index='(index) => { return (index + 1) + (this.pageNum - 1) * this.pageSize }'>
                    </el-table-column>
                    <el-table-column label="部门" show-overflow-tooltip prop="Department" width="120px"></el-table-column>
                    <el-table-column label="资产名称" show-overflow-tooltip prop="DeviceName"
                      width="160px"></el-table-column>
                    <el-table-column label="使用者" show-overflow-tooltip prop="UserName" width="120px"></el-table-column>
                    <el-table-column label="统计时间" prop="TransTime" width="180" show-overflow-tooltip>
                      <template slot-scope="{row}">
                        {{ parseTime(row.TransTime) }}
                      </template>
                    </el-table-column>
                    <el-table-column label="软件分类" prop="Class" show-overflow-tooltip></el-table-column>
                    <el-table-column label="软件名称" prop="ProcName" show-overflow-tooltip></el-table-column>
                    <el-table-column label="安全等级" prop="Level" width="100px" show-overflow-tooltip></el-table-column>
                    <el-table-column label="工作属性" prop="Property" width="100px" show-overflow-tooltip>
                      <template slot-scope="{row}">
                        <span v-show="row.Property === 0">初始</span>
                        <span v-show="row.Property === 1">工作</span>
                        <span v-show="row.Property === 2">娱乐</span>
                        <span v-show="row.Property === 3">其他</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="执行文件" prop="ProcExe" show-overflow-tooltip></el-table-column>
                    <el-table-column label="窗口标题" prop="Title" show-overflow-tooltip></el-table-column>
                    <el-table-column label="执行文件全路径" prop="FullPath" show-overflow-tooltip></el-table-column>
                    <el-table-column label="签名厂商" prop="CertFactory" show-overflow-tooltip></el-table-column>
                  </el-table>
                  <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page=pageNum :page-size=pageSize background
                    layout="total, prev, pager, sizes, next, jumper" :total="total">
                  </el-pagination>
                </el-tab-pane>
              </el-tabs>
            </el-col>
          </el-row>
          <el-dialog :close-on-click-modal="false" :close-on-press-escape=false :pie1Data="pie1Data" top="3vh"
            :pie2Data="pie2Data" :pie3Data="pie3Data" :pie4Data="pie4Data" :tableData="tableData"
            :visible.sync="pdfDialog" title="PDF打印" v-el-drag-dialog width="80%">
            <div style="height: calc(100vh - 200px); overflow-y: auto;">
              <div ref="pdfContent" style="padding: 20px 20px 0 20px;">
                <p style="text-align: left;font-size: 20px;font-weight: 600;" class="title">按资产使用时长统计分析</p>
                <piechart :chartData="pie1Data" title="按资产使用时长统计分析" />
                <p style="text-align: left;font-size: 20px;font-weight: 600;" class="title">按部门使用时长统计分析</p>
                <piechart :chartData="pie2Data" title="按部门使用时长统计分析" />
                <p style="text-align: left;font-size: 20px;font-weight: 600;" class="title">按软件分类使用时长统计分析</p>
                <piechart :chartData="pie3Data" title="按软件分类使用时长统计分析" />
                <p style="text-align: left;font-size: 20px;font-weight: 600;" class="title">按应用软件使用时长统计分析</p>
                <piechart :chartData="pie4Data" title="按应用软件使用时长统计分析" />
                <p style="text-align: left;font-size: 20px;font-weight: 600;" class="title">电脑使用列表</p>
                <el-table stripe border :data="tableData" style="width: 100%;">
                  <el-table-column label="序号" width="60" type="index"
                    :index='(index) => { return (index + 1) + (this.pageNum - 1) * this.pageSize }'> </el-table-column>
                  <el-table-column label="部门" show-overflow-tooltip prop="Department" width="120px"></el-table-column>
                  <el-table-column label="资产名称" show-overflow-tooltip prop="DeviceName" width="160px"></el-table-column>
                  <el-table-column label="使用者" show-overflow-tooltip prop="UserName" width="120px"></el-table-column>
                  <el-table-column label="使用时长" prop="RunningSeconds" width="160" show-overflow-tooltip>
                    <template slot-scope="{row}">
                      {{ formatDuration(row.RunningSeconds) }}
                    </template>
                  </el-table-column>
                  <el-table-column label="软件分类" prop="Class" show-overflow-tooltip></el-table-column>
                  <el-table-column label="软件名称" prop="ProcName" show-overflow-tooltip></el-table-column>
                  <el-table-column label="安全等级" prop="Level" width="100px" show-overflow-tooltip></el-table-column>
                  <el-table-column label="工作属性" prop="Property" width="100px" show-overflow-tooltip>
                    <template slot-scope="{row}">
                      <span v-show="row.Property === 0">初始</span>
                      <span v-show="row.Property === 1">工作</span>
                      <span v-show="row.Property === 2">娱乐</span>
                      <span v-show="row.Property === 3">其他</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="执行文件" prop="ProcExe" show-overflow-tooltip></el-table-column>
                </el-table>
              </div>
            </div>
            <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="exportPDF">打 印</el-button>
              <el-button type="info" @click="pdfDialog = false">关 闭</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import connsearch from "@/components/connSearch";
import eventTrends from './components/BarChart3.vue'  //柱状图
import piechart from "./components/piechart.vue";
import { parseTime, GetDeviceTree2, formatDuration, findValueByKey } from "@/utils/index.js";
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import { connrequest } from "@/api/firstPage.js"
import partitionTree from "@/components/partitionTree";
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';
export default {
  directives: { elDragDialog },
  components: { connsearch, eventTrends, piechart, partitionTree },
  data() {
    let that = this
    return {
      activeName: "",
      tabTitle: '按资产使用时长统计分析',
      tabTitle2: "概要分析",
      containerWidth: 'calc(100% - 275px)',
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tab: false,
      nodeData: [],
      DepartmentList: [],
      expandArray: [0, 100],
      barchartData: { xAxis: [], series: [] },
      pie1Data: [],
      pie2Data: [],
      pie3Data: [],
      pie4Data: [],
      tableData: [],
      tableData2: [],
      pdfDialog: false,
      nodeData: [],
      DepartmentList: [],
      DeviceList: [],
      ClassList: [],
      searchform: {},
      options: {},
      nodeobj: {},
      searchobj: {},
      echartsobj: {},
      detailsobj: {},
      resourceForm: {},
      DeviceObj: {},
      formOptions: [
        {
          col: 12,
          hidden: false,
          label: "日期", // label文字
          prop: "timeRange", // 字段名
          initValue: [], // 字段初始值,
          element: "el-date-picker", // 指定elementui组件
          type: "", // 日期选择类型,
          events: {
            input(val) {
              that.searchform.timeRange = val
            }
          }
        },
        {
          col: 12,
          label: "软件名称",
          prop: "ProcName",
          element: "el-input",
          placeholder: "请输入软件名称",
          maxlength: 50,
          events: {
            input(val) {
              that.searchform.ProcName = val
            }
          }
        },
      ]
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.pageSize = (Cookies.get('Zhuoguan_DefaultPageLength') || 10) * 1;

    let endTime = new Date(new Date().setHours(0, 0, 0, 0)).getTime()
    let startTime = endTime - ((Cookies.get('Zhuoguan_DefaultLogDays') || 7) * 24 * 3600 * 1000);
    let arr = [parseTime(startTime), '']
    this.formOptions.map(item => {
      if (item.label === '日期') {
        item.initValue = arr
      }
    })
    this.$set(this.searchform, 'timeRange', arr)
    this.getData()
    this.GetWindowsSoftClass()
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.initData()
  },
  methods: {
    parseTime,
    formatDuration,
    async initData(searchobj = {}) {
      try {
        const { DeviceTree, departments, devices } = await GetDeviceTree2('WindowProcLog', {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          DeviceUUID: this.searchform.DeviceUUID,
          ProcName: this.searchform.ProcName,
          ...searchobj
        })
        this.nodeData = this.processTreeCounts(DeviceTree, count => formatDuration(count))
        this.DepartmentList = departments
        this.DeviceList = devices
        this.expandArray = [DeviceTree?.[0]?.Name]
      } catch (error) {
        this.$message.error('加载设备树数据失败');
      }
    },
    processTreeCounts(tree, processFn) {
      // 基本校验
      if (!tree || !Array.isArray(tree)) return tree;
      // 递归处理树
      function traverse(node) {
        // 处理当前节点的 Count
        if (node.hasOwnProperty('Count')) {
          node.Count = processFn(node.Count);
        }
        // 递归处理所有子节点
        if (node.Children && Array.isArray(node.Children)) {
          node.Children.forEach(child => traverse(child));
        }
        return node;
      }
      // 处理整个树（支持数组或单个节点）
      if (Array.isArray(tree)) {
        return tree.map(node => traverse({ ...node }));
      }
      return traverse({ ...tree });
    },
    handleNodeClick(nodeData) {
      this.pageNum = 1
      this.nodeobj = { DepartmentID: nodeData.data.isDevice ? nodeData.data.DepartmentID : nodeData.data.ID, DeviceUUID: nodeData.data.isDevice ? nodeData.data.DeviceUUID : '' }
      this.echartsobj = {}
      this.detailsobj = {}
      this.getData({ ...this.searchobj, ...this.nodeobj })
    },
    toggleVisibility(isHidden) {
      if (isHidden)
        this.containerWidth = '100%';
      else
        this.containerWidth = 'calc(100% - 275px)';
    },
    async GetWindowsSoftClass() {
      const res = await connrequest('GetWindowsSoftClass')
      this.ClassList = res.Data
    },
    async detailsChange(row) {
      this.pageNum = 1
      this.pageSize = 10
      this.tabTitle2 = '加载明细'
      this.detailsobj = { ...this.searchobj, ...this.echartsobj, ProcName: row.ProcName, ...this.nodeobj, DeviceUUID: row.DeviceUUID }
      this.GetWindowProc(this.detailsobj)
      switch (this.tabTitle) {
        case '按资产使用时长统计分析':
          this.GetWindowProcSumGroupByDevice(this.detailsobj)
          break;
        case '按部门使用时长统计分析':
          this.GetWindowProcSumGroupByDepartment(this.detailsobj)
          break;
        case '按软件分类使用时长统计分析':
          this.GetWindowProcSumGroupByProcClass(this.detailsobj)
          break;
        case '按应用软件使用时长统计分析':
          this.GetWindowProcSumGroupByProcName(this.detailsobj)
          break;
      }
    },
    async openPDF() {
      this.GetWindowProc({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
      this.GetWindowProcSumGroupByDevice({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
      this.GetWindowProcSumGroupByDepartment({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
      this.GetWindowProcSumGroupByProcClass({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
      this.GetWindowProcSumGroupByProcName({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
      this.pdfDialog = true
    },
    async exportPDF() {
      const element = this.$refs.pdfContent
      const canvas = await html2canvas(element, {
        scale: window.devicePixelRatio, // 提高分辨率
        useCORS: true // 如果有图片，开启跨域
      })
      const imgData = canvas.toDataURL('image/png')
      const pdf = new jsPDF('p', 'mm', 'a4')
      const pageWidth = pdf.internal.pageSize.getWidth()
      const pageHeight = pdf.internal.pageSize.getHeight()
      const imgWidth = pageWidth
      const imgHeight = (canvas.height / canvas.width) * imgWidth
      // 如果内容超出一页，添加分页
      let position = 0
      if (imgHeight < pageHeight) {
        pdf.addImage(imgData, 'PNG', 0, 0, imgWidth, imgHeight)
      } else {
        while (position < imgHeight) {
          pdf.addImage(imgData, 'PNG', 0, -position, imgWidth, imgHeight)
          position += pageHeight
          if (position < imgHeight) pdf.addPage()
        }
      }
      pdf.save('工作绩效统计文件.pdf')
    },
    // 获取所有数据
    getData(searchobj = {}) {
      switch (this.tabTitle2) {
        case '概要分析':
          this.GetEventLog(searchobj)
          break;
        case '加载明细':
          this.GetWindowProc(searchobj)
          break;
      }
      switch (this.tabTitle) {
        case '按资产使用时长统计分析':
          this.GetWindowProcSumGroupByDevice(searchobj)
          break;
        case '按部门使用时长统计分析':
          this.GetWindowProcSumGroupByDepartment(searchobj)
          break;
        case '按软件分类使用时长统计分析':
          this.GetWindowProcSumGroupByProcClass(searchobj)
          break;
        case '按应用软件使用时长统计分析':
          this.GetWindowProcSumGroupByProcName(searchobj)
          break;
      }
    },
    echartsClick(val, title) {
      this.pageNum = 1
      if (title === '按日统计分析') {
        this.echartsobj = { StartTime: `${val} 00:00:00`, EndTime: `${val} 23:59:59` }
        switch (this.tabTitle2) {
          case '概要分析':
            this.GetEventLog({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
            break;
          case '加载明细':
            this.GetWindowProc({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj, ...this.detailsobj })
            break;
        }
      } else if (title === '按资产使用时长统计分析') {
        this.echartsobj = { DeviceUUID: findValueByKey(val, this.DeviceList, 'DeviceName', 'DeviceUUID') }
        switch (this.tabTitle2) {
          case '概要分析':
            this.GetEventLog({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
            break;
          case '加载明细':
            this.GetWindowProc({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj, ...this.detailsobj })
            break;
        }
      } else if (title === '按部门使用时长统计分析') {
        this.echartsobj = { DepartmentID: findValueByKey(val, this.DepartmentList, 'Name', 'ID') }
        switch (this.tabTitle2) {
          case '概要分析':
            this.GetEventLog({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
            break;
          case '加载明细':
            this.GetWindowProc({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj, ...this.detailsobj })
            break;
        }
      } else if (title === '按软件分类使用时长统计分析') {
        this.echartsobj = { Class: val }
        switch (this.tabTitle2) {
          case '概要分析':
            this.GetEventLog({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
            break;
          case '加载明细':
            this.GetWindowProc({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj, ...this.detailsobj })
            break;
        }
      } else {
        this.echartsobj = { ProcName: val }
        switch (this.tabTitle2) {
          case '概要分析':
            this.GetEventLog({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
            break;
          case '加载明细':
            this.GetWindowProc({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj, ...this.detailsobj })
            break;
        }
      }
    },
    tabClick(row) {
      this.tabTitle = row.label
      switch (row.label) {
        case '按资产使用时长统计分析':
          this.GetWindowProcSumGroupByDevice({ ...this.searchobj, ...this.nodeobj, ...this.detailsobj })
          break;
        case '按部门使用时长统计分析':
          this.GetWindowProcSumGroupByDepartment({ ...this.searchobj, ...this.nodeobj, ...this.detailsobj })
          break;
        case '按软件分类使用时长统计分析':
          this.GetWindowProcSumGroupByProcClass({ ...this.searchobj, ...this.nodeobj, ...this.detailsobj })
          break;
        case '按应用软件使用时长统计分析':
          this.GetWindowProcSumGroupByProcName({ ...this.searchobj, ...this.nodeobj, ...this.detailsobj })
          break;
      }
    },
    tabClick2(row) {
      this.pageNum = 1
      this.pageSize = 10
      this.tabTitle2 = row.label
      switch (row.label) {
        case '概要分析':
          this.GetEventLog({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
          break;
        case '加载明细':
          this.GetWindowProc({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
          break;
      }
    },
    // 获取事件日志列表
    async GetEventLog(searchobj = {}) {
      const data = {
        Paging: {
          QueryPageNo: this.pageNum,
          QueryPageLength: this.pageSize
        },
        Filter: {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          DeviceUUID: this.searchform.DeviceUUID,
          ProcName: this.searchform.ProcName,
          ...searchobj
        }
      }
      const res = await connrequest('GetWindowProcSumGroupByDeviceAndProc', data)
      res.Data = res.Data || []
      this.tableData = res.Data
      this.total = res.Paging.TotalCount || 0
    },
    async GetWindowProc(searchobj = {}) {
      const data = {
        Paging: {
          QueryPageNo: this.pageNum,
          QueryPageLength: this.pageSize
        },
        Filter: {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          DeviceUUID: searchobj.DeviceUUID,
          ProcName: searchobj.ProcName,
          ...searchobj
        }
      }
      const res = await connrequest('GetWindowProc', data)
      this.tableData2 = res.Data
      this.total = res.Paging.TotalCount || 0
    },
    // 按资产使用时长统计分析
    async GetWindowProcSumGroupByDevice(searchobj = {}) {
      let arr = []
      const data = {
        Filter: {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          DeviceUUID: this.searchform.DeviceUUID || '',
          DepartmentID: this.searchform.DepartmentID || '',
          ProcName: this.searchform.ProcName,
          ...searchobj
        }
      }
      const res = await connrequest('GetWindowProcSumGroupByDevice', data)
      res.Data = res.Data || []
      res.Data.map(item => {
        arr.push({ name: item.Name || '未知', value: item.Count })
      })
      this.pie1Data = arr
    },
    // 按部门统计时长
    async GetWindowProcSumGroupByDepartment(searchobj = {}) {
      const searchform = this.searchform || {};
      const data = {
        Filter: {
          StartTime: searchform.timeRange?.[0] || '',
          EndTime: searchform.timeRange?.[1] || '',
          DeviceUUID: searchform.DeviceUUID || '',
          DepartmentID: searchform.DepartmentID || '',
          ProcName: searchform.ProcName || '',
          ...searchobj
        }
      };
      const res = await connrequest('GetWindowProcSumGroupByDepartment', data);
      const resData = res?.Data || [];
      this.pie2Data = resData.map(item => ({
        name: item.Name || '未知',
        value: item.Count || 0 // 确保 Count 有默认值
      }));
    },
    // 按软件分类使用时长统计分析
    async GetWindowProcSumGroupByProcClass(searchobj = {}) {
      const searchform = this.searchform || {};
      const data = {
        Filter: {
          StartTime: searchform.timeRange?.[0] || '',
          EndTime: searchform.timeRange?.[1] || '',
          DeviceUUID: searchform.DeviceUUID || '',
          DepartmentID: searchform.DepartmentID || '',
          ProcName: searchform.ProcName || '',
          ...searchobj
        }
      };
      const res = await connrequest('GetWindowProcSumGroupByProcClass', data);
      const resData = res?.Data || [];
      this.pie3Data = resData.map(item => ({
        name: item.Name,
        value: item.Count || 0
      }));
    },
    // 按软件名称统计时长
    async GetWindowProcSumGroupByProcName(searchobj = {}) {
      let arr = []
      const data = {
        Filter: {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          DeviceUUID: this.searchform.DeviceUUID || '',
          DepartmentID: this.searchform.DepartmentID || '',
          ProcName: this.searchform.ProcName,
          ...searchobj
        }
      }
      const res = await connrequest('GetWindowProcSumGroupByProcName', data)
      res.Data = res.Data || []
      res.Data.map(item => {
        arr.push({ name: item.Name || '未知', value: item.Count })
      })
      this.pie4Data = arr
    },
    // 分析今日绩效
    async ParseTodayWindowsProc() {
      const res = await connrequest('ParseTodayWindowsProc')
      if (res.Status.StatusCode === 0) {
        this.$message.success('分析今日绩效成功')
        this.getData()
      }
    },
    // 搜索
    search(val) {
      this.pageNum = 1
      this.searchobj = { ...val }
      this.searchform = { ...val }
      if (this.searchform.timeRange) {
        this.searchobj = { ...this.searchobj, StartTime: this.searchform?.timeRange[0] || '', EndTime: this.searchform?.timeRange[1] || '' }
      }
      this.getData({ ...this.echartsobj, ...this.searchobj, ...this.nodeobj })
      this.initData({ ...this.searchobj })
    },
    // 重置
    reset() {
      let endTime = new Date(new Date().setHours(0, 0, 0, 0)).getTime()
      let startTime = endTime - ((Cookies.get('Zhuoguan_DefaultLogDays') || 7) * 24 * 3600 * 1000);
      let arr = [parseTime(startTime), '']
      this.pageNum = 1
      this.formOptions = this.formOptions.map(item => {
        if (item.label === "日期") {
          item.initValue = arr
        } else {
          item.initValue = ""
        }
        return item
      })
      this.searchform = {}
      this.echartsobj = {}
      this.searchobj = {}
      this.detailsobj = {}
      this.nodeobj = {}
      this.$set(this.searchform, 'timeRange', arr)
      this.$refs.connsearch.addInitValue()
      this.getData()
      this.initData()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.pageNum = 1
      switch (this.tabTitle2) {
        case '概要分析':
          this.GetEventLog({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
          break;
        case '加载明细':
          this.GetWindowProc({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj, ...this.detailsobj })
          break;
      }
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      switch (this.tabTitle2) {
        case '概要分析':
          this.GetEventLog({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
          break;
        case '加载明细':
          this.GetWindowProc({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj, ...this.detailsobj })
          break;
      }
    },
    handleClick() {
      this.tab = !this.tab
    }
  }
};
</script>
<style lang="scss" scoped>
@import "./components/css/conncss.scss";
</style>